/*
 * jxSwing Project (2014)
 * 
 * by Denilson Edinaldo Pinto
 * 
 * LGPL
 * Copyright (C) 2014
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.jxSwing.component.comboBox.cellRenderer;

import java.awt.Component;
import java.util.Locale;

import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;

/**
 * @author Denilson Edinaldo Pinto
 * @porject jxSwing
 * @package org.jxSwing.component.comboBox.cellRenderer
 * @date 15/06/2014 - 15:34:55
 */
public class LocaleListRenderer extends JLabel implements ListCellRenderer<Locale> {
	
	private static final long serialVersionUID = 8584671459672849367L;
	
	private Type type = null;
	
	/**
     * 
     */
	public LocaleListRenderer() {
	
		this(Type.DISPLAY_LANGUAGE);
	};
	
	/**
	 * @param type
	 */
	public LocaleListRenderer(final Type type) {
	
		this.type = type;
		
		setOpaque(true);
		setHorizontalAlignment(LEADING);
		setVerticalAlignment(CENTER);
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.ListCellRenderer#getListCellRendererComponent(javax.swing.JList, java.lang.Object, int, boolean, boolean)
	 */
	@Override
	public Component getListCellRendererComponent(final JList<? extends Locale> list, final Locale value, final int index, final boolean isSelected, final boolean cellHasFocus) {
	
		//
		if(isSelected) {
			
			setBackground(list.getSelectionBackground());
			setForeground(list.getSelectionForeground());
			
		}else {
			
			setBackground(list.getBackground());
			setForeground(list.getForeground());
		}
		
		//
		if(value != null) {
			
			final String country = value.getCountry().isEmpty() ? "" : "-" + value.getCountry();
			final String displayLanguage = value.getDisplayLanguage().substring(0, 1).toUpperCase() + value.getDisplayLanguage().substring(1);
			
			//
			if(type.equals(Type.COUNTRY) || type.equals(Type.DISPLAY_COUNTRY) || type.equals(Type.ISO3_COUNTRY)) {
				
				//
				setText(type.equals(Type.COUNTRY) ? value.getCountry() : type.equals(Type.DISPLAY_COUNTRY) ? value.getDisplayCountry() : value.getISO3Country());
				
			}else {
				
				//
				setText((type.equals(Type.LANGUAGE) ? value.getLanguage() : type.equals(Type.DISPLAY_LANGUAGE) ? displayLanguage : value.getISO3Language()) + country);
			}
			
			final StringBuilder tooltip = new StringBuilder();
			
			tooltip.append("<html>");
			tooltip.append(value.getDisplayCountry());
			tooltip.append(" (" + displayLanguage + ")");
			tooltip.append("</html>");
			
			setToolTipText(tooltip.toString());
		}
		
		return this;
	}
	
	/**
	 * @author Denilson Edinaldo Pinto
	 * @porject jxSwing
	 * @package org.jxSwing.component.comboBox.cellRenderer
	 * @date 15/06/2014 - 15:36:46
	 */
	public static enum Type {
		COUNTRY,
		DISPLAY_COUNTRY,
		ISO3_COUNTRY,
		LANGUAGE,
		DISPLAY_LANGUAGE,
		ISO3_LANGUAGE,
	}
}
